package ru.housewife.form;

import ru.housewife.model.Region;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Form object for
 * @see ru.housewife.model.Region
 *
 * @author Alexander Troshanin
 */
public class RegionForm {

    private long id;

    private String name;

    private boolean supportPriceCalc;

    private RegionForm parent;

    private List<RegionForm> childs;

    private boolean selectable;

    public RegionForm() {
    }

    public RegionForm(Region region, RegionForm parent) {
        id = region.getId();
        name = region.getName();
        this.parent = parent;
        supportPriceCalc = region.isSupportPriceCalc();

        List<Region> childs = region.getChilds();
        if (childs != null && !childs.isEmpty()) {
            this.childs = new ArrayList<RegionForm>();
            for (Region child : region.getChilds()) {
                this.childs.add(new RegionForm(child, this)); 
            }
        } else {
            this.childs = Collections.emptyList();
        }

        selectable = supportPriceCalc || isParentSupportPriceCalc();
    }

    private boolean isParentSupportPriceCalc() {
        return parent != null && (parent.supportPriceCalc || parent.isParentSupportPriceCalc());
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSupportPriceCalc() {
        return supportPriceCalc;
    }

    public RegionForm getParent() {
        return parent;
    }

    public List<RegionForm> getChilds() {
        return childs;
    }

    public boolean isSelectable() {
        return selectable;
    }
}
